﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Hellang.Middleware.ProblemDetails;
using Microsoft.EntityFrameworkCore;
using MyApplication;
using MyAutoMapper;
using MyEFCore;
using MyWebApi.Models;

namespace MyWebApi.Infrostructure;

public static class ServiceExtensions
{
    public static void ConfigureServices(this IServiceCollection services, IWebHostEnvironment env,
        IConfiguration configuration)
    {
        ConfigureCors(services, configuration);
        ConfigureOpenApi(services);
        ConfigureController(services);
        ConfigureDatabase(services, env, configuration);
        ConfigureAutoMapper(services);
        ConfigureGlobalExceptions(services, env);
        ConfigureCustomizedOptions(services, configuration);
    }

    private static void ConfigureCustomizedOptions(IServiceCollection services, IConfiguration configuration)
    {
        services.Configure<PageOptions>(configuration.GetSection("PageOptions"));
    }

    private static void ConfigureGlobalExceptions(IServiceCollection services, IWebHostEnvironment env)
    {
        services.AddProblemDetails(options =>
        {
            options.IncludeExceptionDetails = (context, exception) => env.IsDevelopment();
        });
    }

    private static void ConfigureAutoMapper(IServiceCollection services)
    {
        services.AddAutoMapper(typeof(ProductProfile));
    }

    private static void ConfigureDatabase(IServiceCollection services, IWebHostEnvironment env, IConfiguration configuration)
    {
        services.AddDbContext<MyDbContext>(options =>
        {
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
            if (env.IsDevelopment())
            {
                options.EnableDetailedErrors();
                options.EnableSensitiveDataLogging();
            }
        });
        services.AddScoped<IProductRepository, ProductRepository>();
        services.AddScoped<IRepositoryManager, RepositoryManager>();
        services.AddScoped<IProductService, ProductService>();
    }

    private static void ConfigureController(IServiceCollection services)
    {
        services.AddControllers();
    }

    private static void ConfigureOpenApi(IServiceCollection services)
    {
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen();
    }

    private static void ConfigureCors(IServiceCollection services, IConfiguration configuration)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder =>
                {
                    builder
                        .WithOrigins(configuration.GetSection("CorsPolicy:AllowedOrigins").Get<string[]>())
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .WithExposedHeaders("X-Pagination");
                });
        });
    }
}
